﻿#include "FITKAcousticsPhysicsAdaptor.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractLoad.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"


#include "FITKAcousticsMaterialMgrAdaptor.h"
#include "FITKAcousticsPhysicsMgrAdaptor.h"
#include "FITKAcousticsSpectraMgrAdaptor.h"
#include "FITKAcousticsNCTMgrAdaptor.h"
#include "FITKAcousticsLoadMgrAdaptor.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"

namespace IO
{
    bool FITKAcousticsPhysicsAdaptor::adaptR()
    {
        Acoustics::FITKAcousticsPhysics* phys = dynamic_cast<Acoustics::FITKAcousticsPhysics*>(_dataObj);
        if (phys == nullptr) return false;
        //运行结果
        bool matres = true;
        bool phyres = true;
        bool speres = true;
        bool NCTM = true;
        bool Load = true;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();                   //材料管理器
        Acoustics::FITKAcousticsPhysicsPropManager* phyMgr = phys->getPhysicsPropManager();             //物理属性管理器
        Acoustics::FITKAcousticsSpectraManager* SpectraManager = phys->getFITKAcousticsSpectraManager(); //频谱管理器
        Acoustics::FITKAcousticsNCTManager*NCTManager = phys->getFITKAcousticsNCTManager();              //声学包管理器
        Acoustics::FITKAcousticsLoadManager * LoadManager = phys->getFITKAcousticsLoadManager();          //载荷管理器
        if (matMgr)
        {
            FITKAcousticsMaterialMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsMaterialMgrAdaptor>("FITKAcousticsHDF5", matMgr);
            if (adaptor)
            {
                adaptor->setReader(_reader);
                adaptor->setDataObject(matMgr);
                matres &= adaptor->adaptR();
                delete adaptor;
            }
        }
        if (phyMgr)
        {
            FITKAcousticsPhysicsMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsPhysicsMgrAdaptor>("FITKAcousticsHDF5", phyMgr);//获取物理属性适配器
            if (adaptor)
            {
                adaptor->setReader(_reader);
                adaptor->setDataObject(phyMgr);
                phyres &= adaptor->adaptR();
                delete adaptor;
            }
        }
        if (SpectraManager)
        {
            FITKAcousticsSpectraMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsSpectraMgrAdaptor>("FITKAcousticsHDF5", SpectraManager);//获取频谱适配器
            if (adaptor)
            {
                adaptor->setReader(_reader);
                adaptor->setDataObject(SpectraManager);
                speres &= adaptor->adaptR();
                delete adaptor;
            }
        }
        if (NCTManager)
        {
            FITKAcousticsNCTMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsNCTMgrAdaptor>("FITKAcousticsHDF5", NCTManager);//声学包适配器
            if (adaptor)
            {
                adaptor->setReader(_reader);
                adaptor->setDataObject(NCTManager);
                NCTM &= adaptor->adaptR();
                delete adaptor;
            }
        }
        if (LoadManager)
        {
            FITKAcousticsLoadMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsLoadMgrAdaptor>("FITKAcousticsHDF5", LoadManager);//载荷适配器
            if (adaptor)
            {
                adaptor->setReader(_reader);
                adaptor->setDataObject(LoadManager);
                Load &= adaptor->adaptR();
                delete adaptor;
            }
        }

        return matres && phyres&&speres&&NCTM;
    }

    bool FITKAcousticsPhysicsAdaptor::adaptW()
    {
        Acoustics::FITKAcousticsPhysics* phys = dynamic_cast<Acoustics::FITKAcousticsPhysics*>(_dataObj);//将数据转换为声学数据
        if (phys == nullptr) return false;
        //运行结果
        bool matres = true;
        bool phyres = true;
        bool speres = true;
        bool NCTM = true;
        bool Load = true;
        Acoustics::FITKAcousticsMaterialManager * matMgr = phys->getMaterialManager();                    //材料管理器
        Acoustics::FITKAcousticsPhysicsPropManager * phyMgr = phys->getPhysicsPropManager();              //物理属性管理器
        Acoustics::FITKAcousticsSpectraManager * SpectraManager = phys->getFITKAcousticsSpectraManager(); //频谱管理器
        Acoustics::FITKAcousticsNCTManager * NCTManager = phys->getFITKAcousticsNCTManager();             //声学包管理器
        Acoustics::FITKAcousticsLoadManager * LoadManager = phys->getFITKAcousticsLoadManager();          //载荷管理器

        if (matMgr)
        {
            FITKAcousticsMaterialMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsMaterialMgrAdaptor>("FITKAcousticsHDF5", matMgr);//获取材料适配器
            if (adaptor)
            {
                adaptor->setWriter(_writer);
                adaptor->setDataObject(matMgr);
                matres &= adaptor->adaptW();
                delete adaptor;
            }
        }

        if (phyMgr)
        {
            FITKAcousticsPhysicsMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsPhysicsMgrAdaptor>("FITKAcousticsHDF5", phyMgr);//获取物理属性适配器
            if (adaptor)
            {
                adaptor->setWriter(_writer);
                adaptor->setDataObject(phyMgr);
                phyres &= adaptor->adaptW();
                delete adaptor;
            }
        }

        if (SpectraManager)
        {

            FITKAcousticsSpectraMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsSpectraMgrAdaptor>("FITKAcousticsHDF5", SpectraManager);//获取频谱适配器
            if (adaptor)
            {
                adaptor->setWriter(_writer);
                adaptor->setDataObject(SpectraManager);
                speres &= adaptor->adaptW();
                delete adaptor;
            }

        }

        if (NCTManager)
        {
            FITKAcousticsNCTMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsNCTMgrAdaptor>("FITKAcousticsHDF5", NCTManager);//声学包适配器
            if (adaptor)
            {
                adaptor->setWriter(_writer);
                adaptor->setDataObject(NCTManager);
                NCTM &= adaptor->adaptW();
                delete adaptor;
            }
        }
        if (LoadManager)
        {
            FITKAcousticsLoadMgrAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsLoadMgrAdaptor>("FITKAcousticsHDF5", LoadManager);//载荷适配器
            if (adaptor)
            {
                adaptor->setWriter(_writer);
                adaptor->setDataObject(LoadManager);
                Load &= adaptor->adaptW();
                delete adaptor;
            }
        }


        return matres && phyres && speres && NCTM&&Load;
    }

}


